package com.maserb.monitor.repository.impl;

import com.maserb.monitor.common.IdUtils;
import com.maserb.monitor.database.JdbcTemplateFactory;
import com.maserb.monitor.database.PreparedStatementUtils;
import com.maserb.monitor.entity.DeviceEntity;
import com.maserb.monitor.entity.PointEigenValueEntity;
import com.maserb.monitor.model.EigenValueItemModel;
import com.maserb.monitor.repository.PointEigenValueRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Repository
public class PointEigenValueRepositoryImpl implements PointEigenValueRepository {
    @Autowired
    JdbcTemplateFactory jdbcTemplateFactory;

    @Override
    public void removeByPointId(String pointId, String deviceCode) {
        if (!StringUtils.isEmpty(deviceCode)) {
            JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(deviceCode);
            if (jdbcTemplate != null) {
                String deleteSql = "delete from point_eigenvalue where pointId = ?";
                jdbcTemplate.update(deleteSql, pointId);
            }
        }
    }

    @Override
    public void insert(List<PointEigenValueEntity> eigenList, String deviceCode) {
        JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(deviceCode);
        for (PointEigenValueEntity entity : eigenList) {
            entity.setId(IdUtils.generateId());
            String insertSql = "insert into point_eigenvalue(code, alarmType, baseline, warning1, warning2, danger1, danger2, pointId, id)" +
                    " values(?,?,?,?,?,?,?,?,?)";

            prepareSaveParameters(entity, jdbcTemplate, insertSql);
        }
    }

    @Override
    public List<PointEigenValueEntity> findAllByPointId(String id, String code) {
        JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(code);
        if (jdbcTemplate != null) {
            String findSql = "select * from point_eigenvalue where pointId=?";
            List<PointEigenValueEntity> ret = jdbcTemplate.query(findSql, new Object[]{id}, new BeanPropertyRowMapper<PointEigenValueEntity>(PointEigenValueEntity.class));
            return ret;
        }
        return null;
    }

    private void prepareSaveParameters(PointEigenValueEntity entity, JdbcTemplate jdbcTemplate, String insertSql) {
        jdbcTemplate.update(insertSql, ps -> {
            PreparedStatementUtils.setString(ps, 1, entity.getCode());
            PreparedStatementUtils.setInt(ps, 2, entity.getAlarmType());
            PreparedStatementUtils.setDouble(ps, 3, entity.getBaseline());
            PreparedStatementUtils.setDouble(ps, 4, entity.getWarning1());
            PreparedStatementUtils.setDouble(ps, 5, entity.getWarning2());
            PreparedStatementUtils.setDouble(ps, 6, entity.getDanger1());
            PreparedStatementUtils.setDouble(ps, 7, entity.getDanger2());
            PreparedStatementUtils.setString(ps, 8, entity.getPointId());
            PreparedStatementUtils.setString(ps, 9, entity.getId());
        });
    }
}
